package com.alm.token.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.GlideApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.base.BaseAdapter;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.data.bean.LH20orderHistoryData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.FortunePageView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LH2OrderHistoryActivity extends BaseActivity implements FortunePageView<LH20orderHistoryData> {


    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_clickreload)
    TextView tvClickreload;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;


    private RefreshLoadMoreManager<LH20orderHistoryData.DataBean> manager;
    private List<LH20orderHistoryData.DataBean> dataBeans = new ArrayList<>();
    private int limit = 100, page = 1;
    private boolean isfull = false;

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_lh2orderhistory;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this)
                .setTitle(getString(R.string.title_hint_item13));
        initManager();
        swipeRefreshLayout.setVisibility(View.GONE);
        nodatashowBody.setVisibility(View.VISIBLE);
    }

    private void initManager() {
        OrderAdapter adapter = new OrderAdapter(this);
        manager = new RefreshLoadMoreManager.Builder(this)
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setOnListItemClickListener(new RefreshLoadMoreManager.OnListItemClickListener() {
                    @Override
                    public void onListItemClick(BaseAdapter.BaseItem item, int position) {
//                        if (dataBeansGuanbi != null && dataBeansGuanbi.size() > position) {
//                            Intent intent = new Intent(LH2OrderHistoryActivity.this, QuantitativeOrderDetailActivity.class);
//                            intent.putExtra("databean", dataBeansGuanbi.get(position));
//                            startActivityForResult(intent, ActivityForResultCode);
//                        }
//                        startActivity(QuantitativeOrderDetailActivity.class);
                    }
                })
                .setSwipeRefreshLayout(swipeRefreshLayout)
                .setOnRefreshListener(new RefreshLoadMoreManager.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
//                        showToast("onRefresh");
                        swipeRefreshLayout.setRefreshing(false);
                        loadMoreData();
                    }
                })
                .setOpenLoadMore(true)
                .setOnLoadMoreListener(new RefreshLoadMoreManager.OnLoadMoreListener() {
                    @Override
                    public void onLoadMore() {
                        manager.getAdapter().loadMoreEnd(true);
                        manager.getAdapter().loadMoreFail();
                        showWaitDialog();
                        preenter.getLH2OorderHistoryData(String.valueOf(limit), String.valueOf(page));
                    }
                })
                .build();

        recyclerView.setNestedScrollingEnabled(false);
    }

    @Override
    protected void loadData() {
//        dataBeansGuanbi = MyApp.getDataBeansGuanbi();
//        if (dataBeansGuanbi != null && dataBeansGuanbi.size() != 0) {
//            manager.setData(dataBeansGuanbi);
//        }
        if (dataBeans.size() == 0) {
            swipeRefreshLayout.setVisibility(View.GONE);
            nodatashowBody.setVisibility(View.VISIBLE);
        } else {
            swipeRefreshLayout.setVisibility(View.VISIBLE);
            nodatashowBody.setVisibility(View.GONE);
        }
        preenter. getLH2OorderHistoryData (String.valueOf(limit), String.valueOf(page));
    }


    void loadMoreData() {
        limit=100;
        page=1;
        preenter. getLH2OorderHistoryData (String.valueOf(limit), String.valueOf(page));
    }

    @Override
    public void onClick(View v) {
//        switch (v.getId()) {
//            case R.id.open_btn:
//
//                startActivityForResult(QuantitativeOrderOpenAddActivity.class, ActivityForResultCode);
//
////                startActivity(QuantitativeOrderOpenAddActivity.class);//开启
//                break;
//
//            case R.id.open_supervip:
//                //解除 查询 当期vip订单
//                Intent intent = new Intent(QuantitativeOrderCloseListActivity.this, VipApplyActivity.class);
//                intent.putExtra("intentType", 1);
//                startActivity(intent);
//                break;
//
//        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == ActivityForResultCode) {
//            if (resultCode == QuantitativeOrderOpenAddActivity.AddAppendSucc) {
////                showToast("添加成功，刷新列表");
//                preenter.getUserListLHData("100", "1");
//            }
//            if (resultCode == QuantitativeOrderDetailActivity.AppendSucc) {
////                showToast("添加成功，刷新列表");
//                preenter.getUserListLHData("100", "1");
//            }
//        }


    }

    private List<LH20orderHistoryData.DataBean> dataBeansOld = new ArrayList<>();
    @Override
    public void getData(LH20orderHistoryData lh20orderHistoryData) {
        if (lh20orderHistoryData.getStatus()==0) {

                if (page == 1) {
                    dataBeans.clear();
                }else if(!isfull&&dataBeans.size()>(page-1)*100){
                    dataBeans.removeAll(dataBeansOld);
                }

                if(lh20orderHistoryData.getData().size()==100){
                    isfull=true;
                    page=page+1;
                }else {
                    isfull=false;
                }
                dataBeansOld=lh20orderHistoryData.getData();
                dataBeans.addAll(lh20orderHistoryData.getData());

                for (int i = 0; i < dataBeans.size(); i++) {
                    dataBeans.get(i).setLayoutId(OrderAdapter.LAYOUT_ID);
                }
                manager.setData(dataBeans);

        }else showToast(lh20orderHistoryData.getMsg());

        if (dataBeans.size() == 0) {
            swipeRefreshLayout.setVisibility(View.GONE);
            nodatashowBody.setVisibility(View.VISIBLE);
        } else {
            swipeRefreshLayout.setVisibility(View.VISIBLE);
            nodatashowBody.setVisibility(View.GONE);
        }
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(),PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {
        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {

        showWaitDialog();
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }






    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.tv_clickreload)
    public void onViewClicked() {
        loadMoreData();
    }


    public class OrderAdapter extends BaseAdapter<LH20orderHistoryData.DataBean> {

        public static final int LAYOUT_ID = R.layout.item_lianghua20_historyorder;

        public OrderAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, LH20orderHistoryData.DataBean item, int itemType) {

            ImageView imageView = (ImageView) helper.getView(R.id.img_order_tubiao);
            GlideApp.with(context).load(ConStant.HOST + item.getTubiao())
                    .error(R.drawable.icon_arctic).into(imageView);
            helper.setText(R.id.tv_tubiaoName, item.getTubiaoName());
            helper.setText(R.id.tv_orderstaue, item.getStatus()==0?getString(R.string.lh20_hint_historystaue):"");
            helper.setText(R.id.tv_orderLevel, "AGO L" + item.getOrderLevel());

            LinearLayout xinjiBody = helper.getView(R.id.ly_xinjibody);
            int ll1 = item.getOrderLevel();
            if (ll1 <= xinjiBody.getChildCount()) {
                for (int i = 0; i < xinjiBody.getChildCount(); i++) {//4number  i=0-3
                    if (i < ll1) { //i=0 1 2 3  ll1=1 2 3 4
                        xinjiBody.getChildAt(i).setVisibility(View.VISIBLE);
                    } else {
                        xinjiBody.getChildAt(i).setVisibility(View.GONE);
                    }
                }
            }

            helper.setText(R.id.tv_tianshu, item.getDays() + getString(R.string.lh20_hint_day));
            helper.setText(R.id.tv_ordernumber, item.getOrdersn());
            helper.setText(R.id.tv_addDate, CommonUtil.getDateTimeFromMillisecond3(item.getStartDate()));
        }
    }
}
